class Solution(object):
    def transformArray(self, arr):
        """
        :type arr: List[int]
        :rtype: List[int]
        """
        add_set = set()
        cut_set = set()
        for i in range(1, len(arr) - 1):
            if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
                cut_set.add(i)
            else:
                if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
                    add_set.add(i)
        while cut_set or add_set:
            for i in cut_set:
                arr[i] -= 1
            for i in add_set:
                arr[i] += 1
            cut_set = set()
            add_set = set()
            for i in range(1, len(arr) - 1):
                if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
                    cut_set.add(i)
                else:
                    if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
                        add_set.add(i)
        return arr


data = Solution()
arr = [6,2,3,4]
print(data.transformArray(arr))
